var Login = function () {
    var handleLogin = function () {
        $('.login-form').validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            rules: {
                userName: {
                    required: true
                },
                password: {
                    required: true
                },
                remember: {
                    required: false
                }
            },
            messages: {
                userName: {
                    required: "帐户不能为空."
                },
                password: {
                    required: "密码不能为空."
                },
                validcode:{
                    required:"验证码必须输入。"
                , minlength: $.validator.format("验证码为{0}个字符")

                }
            },
            showErrors: function (errorMap, errorList) {
                return showErrorTooltip(this, errorList);
            },
            submitHandler: function (form) {
                var params = $(form).serialize();
                $.ajax({
                    url: '/api/login',
                    data: params,
                    dataType: "json",
                    type:"post",
                    //contentType:"application/json",
                    success:function(data, textStatus, jqXHR) {
                        if("SUCCESS"==data.status)
                        location.href="dashboard";
                        else
                        alert(data.msg);
                    },
                    error:function(data, textStatus, jqXHR)
                    {
                        alert(textStatus);
                    }
                });
                return false;
            }
        });

        $('.login-form input').keypress(function (e) {
            if (e.which == 13) {
                if ($('.login-form').validate().form()) {
                    $('.login-form').submit();
                }
                return false;
            }
        });
    }

    var handleForgetPassword = function () {
        $('.forget-form').validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "",
            rules: {
                email: {
                    required: true,
                    email: true
                }
            },
            messages: {
                email: {
                    required: "邮箱不能为空."
                }
            },
            showErrors: function (errorMap, errorList) {
                return showErrorTooltip(this, errorList);
            },
            submitHandler: function (form) {
                form.submit();
            }
        });

        $('.forget-form input').keypress(function (e) {
            if (e.which == 13) {
                if ($('.forget-form').validate().form()) {
                    $('.forget-form').submit();
                }
                return false;
            }
        });

        jQuery('#forget-password').click(function () {
            jQuery('.login-form').hide();
            jQuery('.forget-form').show();
        });

        jQuery('#back-btn').click(function () {
            jQuery('.login-form').show();
            jQuery('.forget-form').hide();
        });

    }

    var handleRegister = function () {
        function format(state) {
            if (!state.id) return state.text; // optgroup
            return "<img class='flag' src='../../assets/global/img/flags/" + state.id.toLowerCase() + ".png'/>&nbsp;&nbsp;" + state.text;
        }

        $('.register-form').validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "",
            rules: {
                email: {
                    required: true,
                    email: true
                },
                userName: {
                    required: true,
                    minlength: 5,
                    maxlength: 20
                },
                password: {
                    required: true,
                    minlength: 6,
                    maxlength: 20
                },
                rpassword: {
                    equalTo: "#register_password"
                }
            },
            messages: { // custom messages for radio buttons and checkboxes
                email: {
                    required: "请输入邮箱",
                    email: "请输入合法的邮箱"
                },
                userName: {required: "请输入帐户", minlength: $.validator.format("帐户不能小于{0}个字符"), maxlength: $.validator.format("帐户不能大于{0}个字符")},
                password: {required: "请输入密码", minlength: $.validator.format("密码不能小于{0}个字符"), maxlength: $.validator.format("密码不能大于{0}个字符")},
                rpassword: {
                    required: "请再次输入密码",
                    equalTo: "请重复输入一次上面的密码"
                }
            },
            showErrors: function (errorMap, errorList) {
                return showErrorTooltip(this, errorList);
            },
            submitHandler: function (form) {
                var params = $(form).serialize();
                $.ajax({
                    url: '/api/register',
                    data: params,
                    dataType: "json",
                    type:"post",
                    //contentType:"application/json",
                    success:function(data, textStatus, jqXHR) {
                        if("SUCCESS"==data.status)
                            location.href="login";
                        else
                            alert(data.msg);
                    },
                    error:function(data, textStatus, jqXHR)
                    {
                        alert(textStatus);
                    }
                });
                return false;
            }
        });
        $('.register-form input').keypress(function (e) {
            if (e.which == 13) {
                if ($('.register-form').validate().form()) {
                    $('.register-form').submit();
                }
                return false;
            }
        });
        jQuery('#register-btn').click(function () {
            jQuery('.login-form').hide();
            jQuery('.register-form').show();
        });
        jQuery('#register-back-btn').click(function () {
            jQuery('.login-form').show();
            jQuery('.register-form').hide();
        });
    }
    return {
        //main function to initiate the module
        init: function () {
            handleLogin();
            handleForgetPassword();
            handleRegister();
        }

    };

}();